<?php

namespace app\model;

use think\Model;

class PigDailyEat extends Model
{
    const IS_DELETE_NO = 0;

    /**
     * @Description 根据out_id检查数据是否存在
     * @author: zj
     * @param int $factory_id
     * @param int $out_id
     * @time 2024/03/18 10:04:20
     * @return false|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function checkOutIdIsExist(int $factory_id,int $out_id)
    {
        $info = $this->field('id')
            ->where('factory_id',$factory_id)
            ->where('out_id',$out_id)
            ->where('delete_time',self::IS_DELETE_NO)
            ->find();
        if (!$info) {
            return false;
        }

        return $info['id'];
    }

    /**
     * @Description 获取生猪进食量
     * @author: zj
     * @param int $pigId
     * @param int $start_time
     * @param int $end_time
     * @time 2024/03/20 11:27:54
     * @return float|int
     */
    public function checkFoodIntake(int $pigId,int $start_time = 0,int $end_time = 0)
    {
        $query = $this->where('out_pig_id',$pigId);
        if ($start_time) {
            $query->where('record_time','>=',$start_time);
        }
        if ($end_time) {
            $query->where('record_time','<=',$end_time);
        }
        return $query->sum('feed_weight') ?? 0;
    }

}